/* eslint suggest-no-throw/suggest-no-throw: 0 */
export type NotUndefined<T> = T extends undefined ? never : T

export type Undefinable<T> = T | undefined

function isNotUndefined<T>(input: Undefinable<T>): input is NotUndefined<T> {
  return input !== undefined
}

export function expectNotUndefined<T>(
  input: Undefinable<T>,
  msg: string
): NotUndefined<T> {
  if (isNotUndefined(input)) {
    return input
  }

  throw new TypeError(msg)
}

export function unwrapUndefinable<T>(input: Undefinable<T>): NotUndefined<T> {
  return expectNotUndefined(input, `unwrapping \`undefined\``)
}
